package com.canbot.u05.activity.eat;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.guide.MapMsgBean;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.control.Cmd;
import com.canbot.u05.control.RobotMgr;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.socket.StringMsgBean;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.MyLeftRockerView;
import com.canbot.u05.widget.LoadingDialogUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

/**
 * Created by zmp on 2017/11/20.
 * 充电标记界面
 */

public class EatMarkActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private MyLeftRockerView rockerView;

        private ImageView markIv;

        private Button backBt;

        private ToastTools toastTools;

        private TextView resultTv;

        private boolean isSuccess;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_eat_mark);
                EventBus.getDefault().register(this);
                toastTools = ToastTools.newInstance(this);
                initView();
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                EventBus.getDefault().unregister(this);
        }

        /**
         * 消息发布者位置
         * ReceiveData
         * mHandler
         * case MsgType.MOBILE_PAD_MAP_TASK:
         * com.unisrobot.uurobot_u5.socket.PadSocketManager#mHandler：
         * case MsgType.MOBILE_PAD_MAP_TASK:
         *
         * @param bean
         */
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMapEvent(StringMsgBean bean) {
                if (bean.getMsgType() == MsgType.MOBILE_PAD_MAP_TASK) {
                        String msgData = bean.getMsgData();
                        if (!TextUtils.isEmpty(msgData)) {
                                MapMsgBean mapMsgBean = new Gson().fromJson(msgData, MapMsgBean.class);
                                if (MapMsgBean.GET_ROBOT_CURRENT_POSE == mapMsgBean.getMsgType()) {
                                        if (mapMsgBean.isSuccess()) {

                                                markIv.postDelayed(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                                showResultDialog(true);
                                                        }
                                                }, 1000);
                                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(15));
                                        }
                                        else {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                toastTools.showText("标记失败");
                                                showResultDialog(false);
                                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(16));
                                        }
                                }
                        }
                }
        }

        private void initView() {
                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(14));
                rockerView = (MyLeftRockerView) findViewById(R.id.eat_left_rocker);
                markIv = (ImageView) findViewById(R.id.eat_mark_iv);
                TextView titleTv = (TextView) findViewById(R.id.guide_base_title_tv);
                titleTv.setText("标记充电点");
                backBt = (Button) findViewById(R.id.eat_bt_back);
                markIv.setOnClickListener(this);
                backBt.setOnClickListener(this);
                rockerView.setRockerListener(new MyLeftRockerView.LeftRockerListenerAdapter() {
                        @Override
                        public void onPress(double angle) {
                                handlerAngle(angle);
                        }

                        @Override
                        public void onUpOrCancel() {
                                RobotMgr.getInstance().keyControl(Cmd.Key_tingzhi);
                        }
                });
        }

        private void handlerAngle(double angle) {
                if (angle <= Math.PI / 4 || angle > Math.PI * 7 / 4) {
                        Log.e("handlerAngle", "left");
                        RobotMgr.getInstance().keyControl(Cmd.Key_zuozhuan);
                }
                else if (angle > Math.PI / 4 && angle < Math.PI * 3 / 4) {
                        Log.e("handlerAngle", "top");
                        RobotMgr.getInstance().keyControl(Cmd.Key_qianjin);
                }
                else if (angle >= Math.PI * 3 / 4 && angle < Math.PI * 5 / 4) {
                        Log.e("handlerAngle", "right");
                        RobotMgr.getInstance().keyControl(Cmd.Key_youzhuan);
                }
                else {
                        Log.e("handlerAngle", "bottom");
                        RobotMgr.getInstance().keyControl(Cmd.Key_houtui);
                }
        }
        private Dialog dialogLoading;
        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.eat_mark_iv:
                                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                                setMarkPoint();
                                break;
                        case R.id.eat_bt_back:
                                finish();
                                break;
                        case R.id.eat_result_bt:
                                resultDialog.dismiss();
                                if (isSuccess) {
                                        startActivity(new Intent(this, EatMarkHintTwoActivity.class));
                                        finish();
                                }
                                break;
                }
        }

        private void setMarkPoint() {
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 11 + "");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {


                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                LoadingDialogUtils.closeDialog(dialogLoading);
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.network_error));
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(16));
                        }
                });
        }


        private Dialog resultDialog;

        private void showResultDialog(boolean isSuccess) {
                if (resultDialog == null) {
                        resultDialog = new Dialog(this, R.style.MyDialogStyle);
                        resultDialog.setContentView(R.layout.eat_dialog_result);
                        resultTv = (TextView) resultDialog.findViewById(R.id.eat_result_tv);
                        resultDialog.findViewById(R.id.eat_result_bt).setOnClickListener(this);

                }
                this.isSuccess = isSuccess;
                resultTv.setText(isSuccess ? "标记成功!" : "标记失败!");
                resultDialog.setCancelable(false);
                resultDialog.show();
        }
}
